﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HengTong.Model._Admin;
using HengTong.Model.Db.System;
using HengTong.ViewModel._Admin.FrameworkUserVMs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TiaoTaoHelper.Models;
using TiaoTaoHelper.Wtm.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;

namespace HengTong.Areas._Admin.Controllers
{
    [AuthorizeJwtWithCookie]
    public partial class FrameworkUserController : BaseApiController
    {
        [ActionDescription("Sys.Search")]
        [HttpPost("[action]")]
        public IActionResult SearchFrameworkUser(
            HengTong.ViewModel._Admin.FrameworkUserVMs.FrameworkUserSearcher searcher)
        {
            if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
            {
                return Request.RedirectCall(Wtm).Result;
            }

            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<HengTong.ViewModel._Admin.FrameworkUserVMs.FrameworkUserListVm>();
                vm.Searcher = searcher;
                return Content(vm.GetJson());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Export")]
        [HttpPost("[action]")]
        public IActionResult FrameworkUserExportExcel(
            HengTong.ViewModel._Admin.FrameworkUserVMs.FrameworkUserSearcher searcher)
        {
            if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
            {
                ModelState.AddModelError(" mh", Localizer["_Admin.HasMainHost"]);
                return BadRequest(ModelState.GetErrorJson());
            }

            var vm = Wtm.CreateVM<HengTong.ViewModel._Admin.FrameworkUserVMs.FrameworkUserListVm>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("[action]")]
        public IActionResult FrameworkUserExportExcelByIds(string[] ids)
        {
            if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
            {
                ModelState.AddModelError(" mh", Localizer["_Admin.HasMainHost"]);
                return BadRequest(ModelState.GetErrorJson());
            }

            var vm = Wtm.CreateVM<HengTong.ViewModel._Admin.FrameworkUserVMs.FrameworkUserListVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }

            return vm.GetExportData();
        }

        /// <summary>
        /// 获取用户信息（第三方调用接口）
        /// </summary>
        /// <param name="userId">用户ID，可选。指定时只返回该用户的信息</param>
        /// <param name="userCode">用户工号，可选。指定时只返回该工号对应的用户信息</param>
        /// <param name="phone">手机号码，可选。指定时只返回该手机号对应的用户信息</param>
        /// <param name="lastSyncTime">上次同步时间，可选。用于增量同步，只返回该时间之后创建或更新的用户信息</param>
        /// <returns>返回用户基本信息及其关联的岗位、部门、组织机构信息</returns>
        /// <response code="200">成功返回用户信息列表</response>
        [ActionDescription("获取用户信息三方调用")]
        [HttpGet("[action]")]
        public async Task<Result<List<CommonUserInfoView>>> GetAllUserInfoForThirdParty(
            Guid? userId = null,
            string userCode = null,
            string phone = null,
            DateTime? lastSyncTime = null)
        {
            var vm = Wtm.CreateVM<HengTong.ViewModel._Admin.FrameworkUserVMs.FrameworkUserListVm>();
            var result = await vm.GetAllUserInfoForThirdPartyAsync(userId, userCode, phone, lastSyncTime);
            return result;
        }
    }
}